package org.testis.gui.mvc.model.testdesign;

import org.testis.gui.mvc.model.*;
import org.testis.gui.mvc.controller.testdesign.SearchController;
import org.testis.orm.labelhandler.LabelFilterRefined;
import org.testis.utils.tree.DefaultMutableLabelNode;
import org.testis.utils.tree.MyDefaultMutableTreeNode;
import java.util.LinkedList;
import javax.swing.tree.DefaultTreeModel;

import org.testis.utils.tree.LabelTreeNode;

/**
 *
 * @author Cahit Emir Erel
 */
public class SearchModel extends AbstractModel {

    private String m_lastError = "";
    private Boolean m_selecterVisibility = false;
    private MyDefaultMutableTreeNode m_selectedNode = null;
    private MyDefaultMutableTreeNode m_root = null;
    private String m_properties = "";
    private String m_operation = "";
    private Boolean m_doApplySelection = true;
    private LinkedList<LabelFilterRefined> m_labelFilterList = new LinkedList<LabelFilterRefined>();

    /**
     * Default constructor
     */
    public SearchModel() {
    }

    public void setProperties(String properties) {
        String oldProperties = m_properties;
        m_properties = properties;
        firePropertyChange(SearchController.PROPERTIES,
                oldProperties,
                properties);
    }

    public String getProperties() {
        return m_properties;
    }

    public void setApply(Boolean apply) {
        Boolean oldApply = m_doApplySelection;
        m_doApplySelection = apply;
        firePropertyChange(SearchController.APPLY_SELECTION,
                oldApply,
                apply);
    }

    public Boolean getApply() {
        return m_doApplySelection;
    }

    public void setOperation(String operation) {
        String oldOperation = m_operation;
        m_operation = operation;
        firePropertyChange(SearchController.SELECT_OPERATION,
                oldOperation,
                operation);
    }

    public String getOperation() {
        return m_operation;
    }

    public void setRefreshTree(Boolean refresh) {
        if (refresh != null && refresh) {
            firePropertyChange(SearchController.TREE_REFRESH,
                    false,
                    true);
        }
    }

    public void setMark(MyDefaultMutableTreeNode node) {
        if (node == null) {
            return;
        }
        firePropertyChange(SearchController.MARK_AS_SELECTED,
                null,
                node);
    }

    public void setUnMark(MyDefaultMutableTreeNode node) {
        if (node == null) {
            return;
        }
        firePropertyChange(SearchController.MARK_AS_UNSELECTED,
                null,
                node);
    }

    public void setLoadTree(MyDefaultMutableTreeNode tree) {
        if (tree == null) {
            return;
        }
        m_root = tree;
        firePropertyChange(SearchController.TREE_LOAD,
                null,
                tree);
    }

    public MyDefaultMutableTreeNode getLoadTree() {
        return m_root;
    }

    public MyDefaultMutableTreeNode getSelectedNode() {
        return m_selectedNode;
    }

    public void setSelectedNode(MyDefaultMutableTreeNode selectedNode) {
        MyDefaultMutableTreeNode oldSelectedNode = m_selectedNode;
        m_selectedNode = selectedNode;
        firePropertyChange(SearchController.SELECT_DESIGN_PART,
                oldSelectedNode,
                selectedNode);
    }

    public void setShowSelecter(Boolean selecterVisibility) {
        if (selecterVisibility == null) {
            return;
        }
        Boolean oldSelecterVisibility = m_selecterVisibility;
        m_selecterVisibility = selecterVisibility;
        firePropertyChange(SearchController.SHOW_SELECTER,
                oldSelecterVisibility,
                selecterVisibility);
    }

    public Boolean getShowSelecter() {
        return m_selecterVisibility;
    }

    public LinkedList<LabelFilterRefined> getLabelFilterList() {
        return m_labelFilterList;
    }

    public void setLabelFilterList(LinkedList<LabelFilterRefined> value) {
        if (value == null) {
            return;
        }
        LinkedList<LabelFilterRefined> oldValue = m_labelFilterList;
        m_labelFilterList = value;
        firePropertyChange(SearchController.LABEL_FILTER_LIST_PROPERTY,
                oldValue,
                m_labelFilterList);
    }

    /**
     * @return the m_lastError
     */
    public String getLastError() {
        return m_lastError;
    }

    /**
     * @param m_lastError the m_lastError to set
     */
    public void setLastError(String value) {
        if (value == null) {
            value = new String("");
        }

        String oldValue = m_lastError;
        m_lastError = value;
        firePropertyChange(SearchController.ERROR_PROPERTY,
                oldValue,
                m_lastError);
    }
}
